package com.pucong.gitee.database.backup.database.controller;

import com.pucong.gitee.database.backup.common.entity.BackupDatabase;
import com.pucong.gitee.database.backup.database.service.BackupDatabaseService;
import com.pucong.gitee.database.backup.database.vo.*;
import com.pucong.gitee.database.backup.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author pucong
 * @company www.louddt.com
 * @create 2020-06-23
 */
@RestController
@RequestMapping("/operation/database")
@Api(value = "数据库操作-API ", tags = "3. 数据库操作 API")
public class OperationDatabaseController {

    @Autowired
    private BackupDatabaseService backupDatabaseService;

    @GetMapping("/connectionTestById/{id}")
    @ApiOperation(value = "301连接测试")
    public Res<Integer> connectionTestById(@ApiParam("主键")@PathVariable("id") String id){
        boolean i = backupDatabaseService.connectionTestById(id);
        return Res.ok(i);
    }

    @GetMapping("/connectionTest")
    @ApiOperation(value = "302连接测试")
    public Res<Integer> connectionTest(BackupDatabase backupDatabase){
        boolean i = backupDatabaseService.connectionTest(backupDatabase);
        return Res.ok(i);
    }

    @GetMapping("/getDatabaseList/{databaseId}")
    @ApiOperation(value = "303根据数据库id获取数据库list")
    public Res<ExeSqlResult> getDatabaseList(@ApiParam("数据库id")@PathVariable("databaseId") String databaseId){
        ExeSqlResult exeSqlResult = backupDatabaseService.getDatabaseList(databaseId);
        return Res.ok(exeSqlResult);
    }

    @PostMapping("/renameDatabase")
    @ApiOperation(value = "304根据数据库id和数据库名称修改数据库")
    public Res<Boolean> renameDatabase(@RequestBody RenameDatabase renameDatabase){
        Boolean aBoolean = backupDatabaseService.renameDatabase(renameDatabase.getDatabaseId(), renameDatabase);
        return Res.ok(aBoolean);
    }

    @DeleteMapping("/deleteDatabase")
    @ApiOperation(value = "305根据数据库id和数据库名称删除数据库")
    public Res<Boolean> deleteDatabase(@ApiParam("数据库id") @RequestBody DeleteDatabase deleteDatabase){
        Boolean aBoolean = backupDatabaseService.deleteDatabase(deleteDatabase.getDatabaseId(), deleteDatabase);
        return Res.ok(aBoolean);
    }

    @PostMapping("/closeConnection")
    @ApiOperation(value = "306关闭会话连接")
    public Res<Boolean> closeConnection(@RequestBody CloseDatabaseConnect closeDatabaseConnect){
        Boolean aBoolean = backupDatabaseService.closeConnection(closeDatabaseConnect.getDatabaseId());
        return Res.ok(aBoolean);
    }
}
